﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using  mBook.Models;
using System.Collections.ObjectModel;

namespace mBook.ViewModels
{
    public class EbookViewModel: BaseViewModel
    {
        public EbookViewModel(Ebook ebook, CategoryviewModel group)
            : base(ebook.MediaId, ebook.Name, ebook.Images)
        {
            this.eBook = ebook;
            dateBuy = ebook.DateBuy;
            author = ebook.Author;
            description = ebook.Description;
            encryptUrl = ebook.EncryptUrl;
            typeFile = ebook.FileType;
            isFree = ebook.IsFree;
            numberOfLike = ebook.NumberOfLiked;
            numberOfPaid = ebook.NumberOfPaid;
            numberOfRate = ebook.NumberOfRate;
            page = ebook.Page;
            password = ebook.Password;
            price = ebook.Price;
            publisher = ebook.Publisher;
            rateLevel = ebook.RateLevel;
            release = ebook.Release;
            requireInfo = ebook.RequireInfo;
            summary = ebook.Summary;
            trailerUrl = ebook.TrailerUrl;
            volume = ebook.Volume;
            filePath = ebook.FilePath;

            _group = group;
        }

        //private string _content = string.Empty;
        //public string Content
        //{
        //    get { return this._content; }
        //    set { this.SetProperty(ref this._content, value); }
        //}

        #region properties

        private Ebook eBook;

        public Ebook EBook
        {
            get { return eBook; }
            
        }

        private EpubEbook epubEbook;

        public EpubEbook EpubEbook
        {
            get { return epubEbook; }
            set { epubEbook = value; }
        }

        private String dateBuy;

        public String DateBuy
        {
            get { return dateBuy; }
            set { this.SetProperty(ref this.dateBuy, value); }
        }

        private String author;

        public String Author
        {
            get { return author; }
            set { this.SetProperty(ref this.author, value); }
        }

        private String description;

        public String Description
        {
            get { return description; }
            set {
                
                this.SetProperty(ref this.description, value);
            }
        }

        private String encryptUrl;

        public String EncryptUrl
        {
            get { return encryptUrl; }
            set { this.SetProperty(ref this.encryptUrl, value); }
        }

        private String typeFile;

        public String FileType
        {
            get { return typeFile; }
            set { this.SetProperty(ref this.typeFile, value); }
        }

        private String isFree;

        public String IsFree
        {
            get { return isFree; }
            set { this.SetProperty(ref this.isFree, value); }
        }

        private String numberOfLike;

        public String NumberOfLiked
        {
            get { return numberOfLike; }
            set { this.SetProperty(ref this.numberOfLike, value); }
        }

        private String numberOfPaid;

        public String NumberOfPaid
        {
            get { return numberOfPaid; }
            set { this.SetProperty(ref this.numberOfPaid, value); }
        }

        private String numberOfRate;

        public String NumberOfRate
        {
            get { return numberOfRate; }
            set { this.SetProperty(ref this.numberOfRate, value); }
        }

        private String page;

        public String Page
        {
            get { return page; }
            set { this.SetProperty(ref this.page, value); }
        }


        private String password;

        public String Password
        {
            get { return password; }
            set { this.SetProperty(ref this.password, value); }
        }

        private String price;

        public String Price
        {
            get { return price; }
            set { this.SetProperty(ref this.price, value); }
        }

        private String publisher;

        public String Publisher
        {
            get { return publisher; }
            set { this.SetProperty(ref this.publisher, value); }
        }

        private float rateLevel;

        public float RateLevel
        {
            get { return rateLevel; }
            set { this.SetProperty(ref this.rateLevel, value); }
        }

        private String release;

        public String Release
        {
            get { return release; }
            set { this.SetProperty(ref this.release, value); }
        }

        private String requireInfo;

        public String RequireInfo
        {
            get { return requireInfo; }
            set { this.SetProperty(ref this.requireInfo, value); }
        }

        private String summary;

        public String Summary
        {
            get { return summary; }
            set { this.SetProperty(ref this.summary, value); }
        }

        private String trailerUrl;

        public String TrailerUrl
        {
            get { return trailerUrl; }
            set { this.SetProperty(ref this.trailerUrl, value); }
        }

        private String volume;

        public String Volume
        {
            get { return volume; }
            set { this.SetProperty(ref this.volume, value); }
        }

        private String filePath;

        public String FilePath
        {
            get { return filePath; }
            
        }

        private String percentDownloading = "0";

        public String PercentDownloading
        {
            get { return percentDownloading; }
            set { this.SetProperty(ref this.percentDownloading, value); }
        }

        private CategoryviewModel _group;
        public CategoryviewModel Group
        {
            get { return this._group; }
            set { this.SetProperty(ref this._group, value); }
        }

#endregion 

        

        #region DateBase
        public static async Task<ObservableCollection<EbookViewModel>> GetMyEbooks()
        {


            ObservableCollection<EbookViewModel> ebooks = new ObservableCollection<EbookViewModel>();
            var db = new SQLite.SQLiteAsyncConnection(App.DBPath);

            var query = await db.Table<Ebook>().OrderBy(c => c.Name).ToListAsync();
            foreach (var _ebook in query)
            {
                var ebook = new EbookViewModel(_ebook, null)
                {
                    
                };
                ebook.UniqueId = Utils.Constants.PREFIX_MY_EBOOK_ID + ebook.UniqueId;
                ebooks.Add(ebook);
            }
            return ebooks;
        }

        

        public static async Task<string> SaveEbook(EbookViewModel ebook)
        {
            string result = string.Empty;
            var db = new SQLite.SQLiteAsyncConnection(App.DBPath);

            try
            {
                var existingeEbook = await (db.Table<Ebook>().Where(
                    c => c.MediaId == ebook.EBook.MediaId)).FirstOrDefaultAsync();

                if (existingeEbook != null)
                {
                    //TODO:  update more (lazy :)) 
                    existingeEbook.Name = ebook.Title;
                    existingeEbook.Password = ebook.EBook.Password;
                    existingeEbook.CurrentPage = ebook.eBook.CurrentPage;
                    existingeEbook.FilePath = ebook.EBook.FilePath;

                    int success = await db.UpdateAsync(existingeEbook);
                }
                else
                {
                    int success = await db.InsertAsync(new Ebook()
                    {
                        MediaId = ebook.EBook.MediaId,
                        DateBuy = ebook.DateBuy,
                        Author = ebook.Author,
                        CategoryId = ebook.eBook.CategoryId,
                        Description = ebook.Description,
                        EncryptUrl = ebook.EncryptUrl,
                        FileType = ebook.FileType,
                        Images = ebook.eBook.Images,
                        IsFree = ebook .IsFree ,
                        Name = ebook.Title,
                        NumberOfLiked = ebook .NumberOfLiked,
                        NumberOfPaid = ebook .NumberOfPaid ,
                        NumberOfRate = ebook .numberOfRate ,
                        Page = ebook .Page ,
                        Password = ebook .EBook.Password ,
                        Price = ebook .Price ,
                        Publisher = ebook .Publisher,
                        RateLevel = ebook.RateLevel,
                        Release = ebook.Release,
                        RequireInfo = ebook .RequireInfo,
                        Summary = ebook.Summary,
                        TrailerUrl = ebook .TrailerUrl,
                        Volume = ebook.Volume,
                        FilePath = ebook.EBook.FilePath,
                       
                    });
                }
                result = "Success";
            }
            catch
            {
                result = "This customer was not saved.";
            }
            return result;
        }

        
        #endregion


    }
}
